<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

use app\common\library\Email;
// use think\Addons;
class Gbook extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function send_email($params)//&$params
    {
        //$params['title'] = '随便定义';
        // $params['email'] = '496631085@qq.com';//收件箱    发件人配置在后台里面设置
        // $params['content'] = 'XiaoHe 网站制作建设 软件开发 二次开发 爬虫 ';
        $email = new Email();
        $result = $email->to($params['email'])
            ->subject('请查收你的留言信息'.$params['title'])
            ->message($params['content'])
            ->send();
        return $result;//返回逻辑
    }

    public function index()
    {	

    	$param = $this->request->param();
    /*	if($this->request->isPost()){
    		$data['tel'] = $param['tel'];
    		$data['shopname'] = $param['shopname'];
    		$data['xihuan'] = $param['xihuan'];
    		$data['manyi'] = $param['manyi'];
    		// $data['name'] = $param['name'];
    		$data['jianyi'] = $param['jianyi'];
    		$data['ip'] = $this->request->ip();
    		// $data['content'] = $param['content'];
    		// $data['ps'] = $param['ps'];

    		$data['createtime'] = time();

    		$charu= Db::name('gbook2')->insert($data);
			if($charu){
				return 'ok';
			}
    		// $res = Db::name('gbook2')->where('id','>',0)->select();
    		// dump($res);
    	}*/
    	// dump();
        if(!empty($param['id'])){
           $cate = Db::name('cate')->where('id',$param['id'])->where('status',1)->find();
           if($cate){
                $this->view->assign('cate',$cate);
               $this->view->assign('cid',$param['id']);
               return $this->view->fetch();
           }else{
             $this->error('未有此链接');
           }
           
        }else{
            $this->error('未有此链接请联系管理员');
            exit();
        }

        
    }
    //没做加密  防刷，以最快的方式写能运行即可的代码！（毕竟只要求快不要求质量，判断有空再加吧）
    public function add(){
        $param = $this->request->param();
        // dump($param);
        if($this->request->isPost()){
            
            $bitianstr = config('site.bitian');
            // $bitianarr = explode('|', $bitianstr);
           
            $cid = $param['cid'];
            $cidres = Db::name('question')->where('cid',$cid)->find();//是否有这个提问
            if($cidres){

                $user = Db::name('answer')->max('user');//最大值
                $num = 0;
                $content = '';
                foreach ($param['list'] as $key => $val) {
                    $data['adminid'] = $cidres['adminid'];
                    $data['user'] = $user+1;

                    $data['question'] = $val['question'];
                    $data['question_id'] = $val['cid'];
                    $data['qstatus'] = $val['qstatus'];
                    if(!empty($val['input'])){
                        $data['content'] = $val['input'];
                    }else{
                        if(strpos($bitianstr,$val['question'])){
                           return json_encode(['status'=>-1,'msg'=>$val['question']."必填"]);
                        }
                        $data['content'] = "";
                    }
                    $data['ip'] = $this->request->ip();
                    $data['createtime'] = time();
                    $charu= Db::name('answer')->insert($data);
                    if($charu){
                        $num++;
                   }
                   $content = $content.'----'.$val['question'].'：'.$data['content'];
                }
                if($num>0){
                    $cate = Db::name('cate')->where('id',$cid)->find();
                    $this->send_email(['email'=>$cate['email'],'title'=>$cate['title'],'content'=>$content]);
                }
                return json_encode(['status'=>$num,'msg'=>"OK"]);

                
               
            }
            
            //我也不报错不给返回
            
            // $data['content'] = $param['content'];
            // $data['ps'] = $param['ps'];

            

            
            
            // $charu= Db::name('answer')->insert($data);
            // if($charu){
            //     return 'ok';
            // }
            // $res = Db::name('gbook2')->where('id','>',0)->select();
            // dump($res);
        }
    }

    public function news()
    {
        $newslist = [];
        return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
    }
    public function yydd()
    {   
        hait("暂未开发");
    	$param = $this->request->param();
    	if($this->request->isPost()){
    		$data['tel'] = $param['tel'];
    		$data['shopname'] = $param['shopname'];
    		$data['sex'] = $param['sex'];
    		$data['address'] = $param['address'];
    		$data['name'] = $param['name'];
    		$data['shoptime'] = strtotime($param['shoptime']); 
    		$data['ip'] = $this->request->ip();
    		// $data['content'] = $param['content'];
    		// $data['ps'] = $param['ps'];

    		$data['createtime'] = time();

    		$charu= Db::name('yuyue')->insert($data);
			if($charu){
				return 'ok';
			}
    		// $res = Db::name('gbook2')->where('id','>',0)->select();
    		// dump($res);
    	}
    	// dump();
        return $this->view->fetch();
    }

}
